<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class Clients extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
      // Call the Model constructor
      parent::_construct();
	}
	function get_list()
	{
		$page = (isset($_POST['page'])) ? (int)$_POST['page'] : 1; // POST the requested page
		$limit = (isset($_POST['rows'])) ? (int)$_POST['rows'] : 10; // POST how many rows we want to have into the grid
		$sidx = (isset($_POST['sidx'])) ? $_POST['sidx'] : 1; // POST index row - i.e. user click to sort
		$sord = (isset($_POST['sord'])) ? $_POST['sord'] : 'asc'; // POST the direction
		if($sord != 'asc' && $sord != 'desc') $sord = 'asc';

		$aFields = array('id', 'site', 'email');
		if(!in_array($sidx, $aFields))$sidx = 'id';

		if(!$sidx) $sidx =1;

		
		$query = "SELECT COUNT(*) AS count FROM users where admin=1";
		$result=$this->db->query($query);
		if($result->num_rows()>0)
		{
			$row=$result->row();
			$count=$row->count;
			$total_pages = ceil($count/$limit);
		}
		else
		{
			$count=0;
			$total_pages = 0;
		}


		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		$SQL = "SELECT id, site, email FROM users where admin=1 ORDER BY $sidx $sord LIMIT $start , $limit";
		//mysql_freeresult($result);
		$result = $this->db->query($SQL);

		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		$i=0;

		foreach($result->result() as $row)
		{
		    $responce->rows[$i]['id']=$row->id;
		    $responce->rows[$i]['cell']=array($row->id,$row->site,$row->email);
		    $i++;
		}
		$this->db->close();
		return json_encode($responce);
		

	}
	function getUserList()
	{
		$query="Select email,site from users where admin=0 and id in (Select idPodp from podpisannie where idUser=".$_SESSION['idUser'].")";
		$this->log($query);
		$result=$this->db->query($query);
		$i=0;
		foreach($result->result() as $row)
		{
			$responce->rows[$i]['id']=$i;
			$responce->rows[$i]['cell']=array($row->email,$row->site);
			$i++;	
		}
		return json_encode($responce);
	}
	function getAgentList()
	{
		$query="Select country,email,site from users where admin=1 and id in (Select idUser from podpisannie where idPodp=".$_SESSION['idUser'].")";
		$this->log($query);
		$result=$this->db->query($query);
		$i=0;
		foreach($result->result() as $row)
		{
			$responce->rows[$i]['id']=$i;
			$responce->rows[$i]['cell']=array($row->country,$row->email,$row->site);
			$i++;
		}
		return json_encode($responce);
	}
	function getOtpr($idPrin=0)
	{
		$query="select idPodp from podpisannie where idUser=$idPrin";
		$result=$this->db->query($query)->result_array();
		//print_r($result[0]);
		$query="select id,site,email from users where admin=0";
		$list=$this->db->query($query);
		//$responce=array();
		$i=0;
		foreach($list->result() as $row)
		{
			$responce2[$i]['cell'][0]=$row->id;
			$responce2[$i]['cell'][2]=$row->site;
			$responce2[$i]['cell'][3]=$row->email;
			$responce2[$i]['cell'][1]='No';
			if(count($result)>0)
			{
			if( in_array($row->id, $result[0]) )
			{$responce2[$i]['cell'][1]='Yes';}
			}
			
			$responce2[$i]['cell'][4]=$idPrin;
			$i++;
		}
		
		$responce->page = 1;
		$responce->total = 1;
		$responce->records = $i;
		$responce->rows=$responce2;
		//$r['rows']=$responce;
		return json_encode($responce);
	}
	function setOtpr($Id,$idOtpr,$check)
	{
		if($check=='No')
		{
			$query='Delete from podpisannie where idUser='.$idOtpr.' and idPodp='.$Id;
			$this->db->query($query);
			//$query="delete from listofservices where idUser=".$Id." and idService in (select idservice from listofservices where idUser=".$idOtpr.")";
			//$this->db->
		}
		else if($check=='Yes')
		{
			$query='insert into podpisannie (idUser,idPodp) values ('.$idOtpr.','.$Id.')';
			$this->db->query($query);
			//$query="";
		}
	}
}